<template>
  <a-modal :title="modal.title" :visible="modal.visible" maskClosable width="600px" @cancel="$emit('done')"
    @ok="submit">
    <a-form :form="form" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
      <a-form-item label="id" style="display: none;">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item label="productId" style="display: none;">
        <a-input v-decorator="['productId']" />
      </a-form-item>

      <a-form-item label="保障期类型">
        <a-radio-group v-decorator="['indemnifyType', { rules: [{ required: true, message: '请选择【保障期类型】' }] }]">
          <a-radio :value="1">终身</a-radio>
          <a-radio :value="2">岁</a-radio>
          <a-radio :value="3">年</a-radio>
        </a-radio-group>
      </a-form-item>

      <a-form-item label="保障期值">
        <a-input-number :min="0" :precision="0"
          v-decorator="['indemnifyValue', { rules: [{ required: true, message: '请输入【保障期值】' }] }]" placeholder="请输入【保障期值】"
          :style="{ width: '100%', textAlign: 'center', display: 'inline-block' }" />
      </a-form-item>

      <a-form-item label="排序号">
        <a-input-number :min="1" :precision="0"
          v-decorator="['sortOrder', { rules: [{ required: true, message: '请输入【排序号】' }] }]"
          :style="{ width: '100%', textAlign: 'center', display: 'inline-block' }" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
  import { add, update } from '@/api/aibao/product/indemnify'

  import pick from 'lodash.pick'
  const fields = ['id', 'productId', 'indemnifyType', 'indemnifyValue', 'sortOrder']

  export default {
    props: {
      modal: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        form: this.$form.createForm(this)
      }
    },
    created() {
      fields.forEach(v => this.form.getFieldDecorator(v))
      this.$watch('modal', () => {
        if (this.modal) {
          this.form.setFieldsValue(pick(this.modal.form, fields))
        }
      }, { deep: true })
    },
    methods: {
      submit() {
        this.form.validateFields((errors, values) => {
          if (!errors) {
            const api = values.id ? update : add
            api(values)
              .then(res => {
                this.$message.success('操作成功')
                this.$emit('done')
              })
              .catch(() => {})
          }
        })
      },
    },
  }
</script>

<style>
</style>